const qiniu = require('qiniu')
const nanoid = require('nanoid')
const config = require('../config')

const mongoose = require('mongoose')
const Movie = mongoose.model('Movie')
const Category = mongoose.model('Category')

const bucket = config.qiniu.bucket
const mac = new qiniu.auth.digest.Mac(config.qiniu.AK, config.qiniu.SK)
const cfg = new qiniu.conf.Config()
const client = new qiniu.rs.BucketManager(mac, cfg)

const uploadToQiniu = async(url, key) => {
    return new Promise((resolve, reject) => {
        client.fetch(url, bucket, key, (err, ret, info) => {
            if (err) {
                reject(err)
            } else {
                if (info.statusCode === 200) {
                    resolve({ key })
                } else {
                    reject(info)
                }
            }
        })
    })
}

;
(async() => {

    // let movies = [{
    //     video: 'http://vt1.doubanio.com/201804131458/b680875571cf0c2fcb3742b613c54fd0/view/movie/M/302280241.mp4',
    //     doubanId: '26997532',
    //     cover: 'https://img3.doubanio.com/img/trailer/medium/2518174173.jpg?',
    //     poster: 'https://img1.doubanio.com/view/photo/l_ratio_poster/public/p2516356597.jpg'
    // }]
    let movies = await Movie.find({
        $or: [
            { videoKey: { $exists: false } },
            { videoKey: null },
            { videoKey: '' }
        ]

    })

    for (let i = 0; i < movies.length; i++) {
        let movie = movies[i]

        // movies.map(async movie => {
        // console.log(movie)
        if (movie.video && !movie.videoKey) {
            try {

                console.log('开始传 video');
                let videoData = await uploadToQiniu(movie.video, nanoid() + '.mp4')
                console.log('开始传 cover')
                let coverData = await uploadToQiniu(movie.cover, nanoid() + '.png')
                console.log('开始传 poster')
                let posterData = await uploadToQiniu(movie.poster, nanoid() + '.png')
                // console.log(videoData)
                if (videoData.key) {
                    movie.videoKey = videoData.key
                }

                if (coverData.key) {
                    movie.coverKey = coverData.key
                }

                if (posterData.key) {
                    movie.posterKey = posterData.key
                }

                console.log(movie)

                //{ video: 'http://vt1.doubanio.com/201804131458/b680875571cf0c2fcb3742b613c54fd0/view/movie/M/302280241.mp4',
                //   doubanId: '26997532',
                //   cover: 'https://img3.doubanio.com/img/trailer/medium/2518174173.jpg?',
                //   poster: 'https://img1.doubanio.com/view/photo/l_ratio_poster/public/p2516356597.jpg',
                //   videoKey: 'http://video.izqiang.com/Rn9adD105Bx~0r8jf5C__.mp4',
                //   coverKey: 'http://video.izqiang.com/RWEV2ipBufeSVR7OLzvjZ.png',
                //   posterKey: 'http: //video.izqiang.com/rQVIN6GCvhjbgGZe493uq.png' 
                // }

                await movie.save()


            } catch (err) {
                console.log(err)
            }
        }
    }

    // })


})()